<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<div id="t">This is a <span>test <span>test <span>test</span></span></span> </div>
</body>
<script>
	var t = document.getElementById("t");
	var tt = t.childNodes;
	var str = "";
	for(var i = 0; i<tt.length; i++){
		if (tt[i].nodeType==3) {
			str+=tt[i].nodeValue;
		} else if(tt[i].nodeType==1){
			str+=tt[i].innerHTML;
		}
	}
	var a = t.innerText;
	console.info(a);
	console.info(str);
	var str1 = "";
	// 不知道元素内嵌套子元素的层级数,可以使用递归,循环使用函数,直到不含有子级元素
	function test(obj) {
		var children = obj.childNodes;
		for (var i = 0; i < children.length; i++) {
			if (children[i].nodeType==3) {
				str1+=children[i].nodeValue;
			} else if (children[i].nodeType==1) {
				return test(children[i]);
			}
		}
	}
	test(t);
	console.info(str1);
</script>
</html>